package com.zjuee.web.ledger.controller;

import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.base.valid.Insert;
import com.zjuee.base.valid.Update;
import com.zjuee.service.PlanCarryService;
import com.zjuee.service.PlanService;
import com.zjuee.base.model.common.plan.PlanCarry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_MAIN_PLAN_URL_PREFIX)
public class PlanCarryController{
    @Resource
    private PlanCarryService planCarryService;
    @Resource
    private PlanService planService;
    @Resource
    private LoginDto loginDto;

    /**
     * 新增转移计划 PlanCarry
     *
     * @param planCarry PlanCarry
     * @return Long
     */
    @PostMapping("/carry/add")
    public R<Long> addPlanCarry(@Validated(Insert.class) @RequestBody  PlanCarry planCarry) {
        if (planCarry.getWpiId() == null)
            return R.fail("计划id不能为空");
        if (planCarry.getWpcModel() == null)
            return R.fail("转移类型不能为空");
        if (planCarry.getWpcSource() == null)
            return R.fail("计划类型不能为空");
        if (planCarry.getWpcDisposeType() == null)
            return R.fail("处置方式不能为空");
        if (planCarry.getWpcDisposeStype() == null)
            return R.fail("处置方式（小类）不能为空");
        if (planCarry.getWpcType() == null)
            return R.fail("废物类别不能为空");
        if (planCarry.getWpcBcode() == null)
            return R.fail("废物代码（大类）不能为空");
        if (planCarry.getWpcScode() == null)
            return R.fail("废物代码（小类）不能为空");
        if (planCarry.getWpcThisDispose() == null)
            return R.fail("本年度计划处置量不能为空");
        if (planCarry.getWpcDisposeTreaty() == null)
            return R.fail("处置协议不能为空");
        return R.ok(planCarryService.add(planCarry));
    }

    /**
     * 修改转移计划 PlanCarry
     *
     * @param planCarry PlanCarry
     * @return Boolean
     */
    @PutMapping("/carry/update")
    public R<Boolean> updatePlanCarry(@Validated(Update.class) @RequestBody  PlanCarry planCarry) {
        if (planCarry.getWpiId() == null) {
            return R.fail("计划ID为空！");
        }
        return R.ok(planCarryService.update(planCarry));
    }

//     
//    public R<Boolean> deletePlanCarry(@NotNull Long id, @NotNull String wpiYear) {
//        Boolean type = false;
//        if (wpiYear.equals(DateUtils.getYear(new Date()))) {
//            type = planCarryService.delete(id, loginDto.getPlanId());
//        } else {
//            PlanInfo plan = planService.selectInfoByBciId(loginDto.getOrgBusinessId(), wpiYear);
//            if (plan != null) {
//                type = planCarryService.delete(id, plan.getId());
//            } else {
//                return R.fail("删除转移计划失败");
//            }
//        }
//        if (type) {
//            return R.ok("success");
//        }
//        return R.fail("删除转移计划失败");
//    }


    /**
     * 根据企业ID获取当年转移计划
     *
     * @param
     * @return
     */
    @GetMapping("/carry/carryListByBciId")
    public R<List<PlanCarry>> carryListByBciId() {

        return R.ok(planCarryService.findPlanCarrylistByBciId(loginDto.getOrgBusinessId(), DateUtils.getYear(new Date())));
    }
}
